package com.syqc.base

import com.syqc.comlib.auto.AutoDisposeViewModel
import com.syqc.monitor.R
import com.syqc.utils.TimeUtil
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.subjects.BehaviorSubject

/**
 *@Author Jasper
 *@Time   2020/9/19
 *@Desc   时间选择ViewModel
 */
open class ChooseTimeViewModel(
    private val isSimple: Boolean = false,
    private val isThirty: Boolean = false,
    isSimpleThirty: Boolean = false
) : AutoDisposeViewModel() {
    private val hourTime = 3600000L
    private val dayTime = 24 * hourTime

    //是否是开始时间
    var isStart = true

    //开始时间
    private val startTime by lazy { BehaviorSubject.create<String>() }
    fun startObserve(): Observable<String> = startTime.hide()

    //结束时间
    private val endTimeSub by lazy { BehaviorSubject.create<String>() }
    fun endObserve(): Observable<String> = endTimeSub.hide()

    val timeList by lazy { TimeUtil.instance.initChooseTime(isThirty) }

    init {
        if (isThirty) {
            if (isSimple) {
                startSimpleDayTime(0)
                endSimpleDayTime(0)
            } else {
                if (isSimpleThirty) {
                    startTime()
                    endTime()
                } else {
                    startDayTime(0)
                    endDayTime(0)
                }
            }
        } else {
            if (isSimple) {
                startSimpleDayTime(0)
                endSimpleDayTime(0)
            } else {
                startTime()
                endTime()
            }
        }
    }

    private fun nowTime() = System.currentTimeMillis()
    fun startTime() {
        startTime.onNext(
            "${TimeUtil.instance.simpleTime(nowTime())} 00:00"
        )
    }

    fun endTime() {
        endTimeSub.onNext(TimeUtil.instance.normalTime(nowTime()))
    }

    private fun endSimpleTime() {
        endTimeSub.onNext(TimeUtil.instance.simpleTime(nowTime()))
    }

    private fun startDayTime(day: Int) {
        startTime.onNext(
            "${
                TimeUtil.instance.simpleTime(nowTime() - day * dayTime)
            } 00:00"
        )
    }

    private fun endSimpleDayTime(day: Int) {
        endTimeSub.onNext(TimeUtil.instance.simpleTime(nowTime() - day * dayTime))
    }

    private fun startSimpleDayTime(day: Int) {
        startTime.onNext(TimeUtil.instance.simpleTime(nowTime() - day * dayTime))
    }

    private fun endDayTime(day: Int) {
        endTimeSub.onNext(
            "${
                TimeUtil.instance.simpleTime(nowTime() - day * dayTime)
            } 23:59"
        )
    }

    private fun startTime(hour: Int) {
        startTime.onNext(TimeUtil.instance.normalTime(nowTime() - hour * hourTime))
    }

    fun nowTimeFormat(): String {
        return if (isSimple)
            TimeUtil.instance.simpleTime(nowTime())
        else
            TimeUtil.instance.normalTime(nowTime())
    }


    fun chooseTime(position: Int) {
        val chooseTime = timeList[position]
        if (isThirty) {
            if (isSimple) {
                when (chooseTime.timeType) {
                    2 -> {
                        startSimpleDayTime(chooseTime.timeNum)
                        endSimpleDayTime(chooseTime.timeNum)
                    }

                    else -> {
                        startSimpleDayTime(chooseTime.timeNum)
                        endSimpleTime()
                    }
                }
            } else {
                when (chooseTime.timeType) {
                    2 -> {
                        startDayTime(chooseTime.timeNum)
                        endDayTime(chooseTime.timeNum)
                    }

                    else -> {
                        startDayTime(chooseTime.timeNum)
                        endTime()
                    }
                }
            }
        } else {
            if (isSimple) {
                when (chooseTime.timeType) {
                    1 -> {
                        startSimpleDayTime(0)
                        endSimpleDayTime(0)
                    }

                    2 -> {
                        startSimpleDayTime(chooseTime.timeNum)
                        endSimpleDayTime(chooseTime.timeNum)
                    }

                    3 -> {
                        startSimpleDayTime(chooseTime.timeNum)
                        endSimpleTime()
                    }

                    4 -> {
                        startSimpleDayTime(chooseTime.timeNum)
                        endSimpleDayTime(1)
                    }
                }
            } else {
                when (chooseTime.timeType) {
                    1 -> {
                        startTime()
                        endTime()
                    }

                    2 -> {
                        startDayTime(chooseTime.timeNum)
                        endDayTime(chooseTime.timeNum)
                    }

                    3 -> {
                        startTime(chooseTime.timeNum)
                        endTime()
                    }

                    4 -> {
                        startDayTime(chooseTime.timeNum)
                        endDayTime(1)
                    }
                }
            }

        }

    }

    fun isOkStart(startTime: String, endTime: String): Boolean {
        val time: Long = TimeUtil.instance.getDiffTime(startTime, endTime, isSimple)
        if (time < 0) {
            toast(R.string.toast_start_less_than_end_time)
            return true
        }
        return false
    }

    /**
     * 判断时间是否合理
     */
    fun isTimeOk(startTime: String, endTime: String): Boolean {
        val time: Long = TimeUtil.instance.getDiffTime(startTime, endTime, isSimple)
        if (time < 0) {
            toast(R.string.toast_start_less_than_end_time)
            return true
        }
        if (isThirty) {
            //判断时差是否大于30天
            if (time >= 2678400000) {
                toast(R.string.toast_more_than_thirty_time)
                return true
            }

        } else {
            //判断时差是否大于7天
            if (time > 604800000) {
                toast(R.string.toast_more_than_seven_time)
                return true
            }
        }

        return false
    }
}